package com.baimsg.microcomponents.receiver

import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.widget.RemoteViews
import com.baimsg.basic.utils.KEY_CHANNEL_NAME
import com.baimsg.basic.utils.KEY_COUNT
import com.baimsg.basic.utils.KEY_STATUS
import com.baimsg.basic.utils.KEY_TYPE
import com.baimsg.basic.utils.KEY_USER_ID
import com.baimsg.basic.utils.POC_ACTION
import com.baimsg.microcomponents.R


/**
 * Create by Baimsg on 2023/11/17
 *
 **/
class PocWidgetReceiver : AppWidgetProvider() {

    private val TAG by lazy {
        this::class.java.simpleName
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        Log.e(TAG, "onReceive: ${
            intent?.run {
                "${this.action}\n${this.extras}"
            }
        }")
        super.onReceive(context, intent)
        context ?: return
        intent ?: return
        if (intent.action == POC_ACTION) {
            val appWidgetManager = AppWidgetManager.getInstance(context)
            val remoteViews = RemoteViews(context.packageName, R.layout.poc_loading_appwidget)
            val userId = intent.getStringExtra(KEY_USER_ID)
            val count = intent.getStringExtra(KEY_COUNT)
            val channelName = intent.getStringExtra(KEY_CHANNEL_NAME)
            val status = intent.getStringExtra(KEY_STATUS)
            remoteViews.setImageViewResource(
                R.id.iv_status, when (intent.getIntExtra(KEY_TYPE, 0)) {
                    1 -> R.drawable.status_idle
                    2 -> R.drawable.status_talk
                    else -> R.drawable.status_offline
                }
            )
            remoteViews.setTextViewText(R.id.tv_user_id, userId)
            remoteViews.setTextViewText(R.id.tv_count, count)
            remoteViews.setTextViewText(R.id.tv_name, channelName)
            remoteViews.setTextViewText(R.id.tv_status, status)
            appWidgetManager.updateAppWidget(
                ComponentName(context, PocWidgetReceiver::class.java), remoteViews
            )
        }
    }

    override fun onEnabled(context: Context?) {
        super.onEnabled(context)
    }

    override fun onAppWidgetOptionsChanged(
        context: Context?,
        appWidgetManager: AppWidgetManager?,
        appWidgetId: Int,
        newOptions: Bundle?,
    ) {
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions)
    }

    override fun onUpdate(
        context: Context?,
        appWidgetManager: AppWidgetManager?,
        appWidgetIds: IntArray?,
    ) {
        Log.e(TAG, "onUpdate: ")
        context ?: return
        super.onUpdate(context, appWidgetManager, appWidgetIds)
        appWidgetIds?.forEachIndexed { _, i ->
            val remoteViews = RemoteViews(context.packageName, R.layout.poc_loading_appwidget)
            remoteViews.setTextViewText(R.id.tv_count, "0/0")
            remoteViews.setImageViewResource(R.id.iv_status, R.drawable.status_offline)
            remoteViews.setTextViewText(R.id.tv_name, "正在登录中...")
            appWidgetManager?.updateAppWidget(i, remoteViews)
        }
    }

    override fun onDeleted(context: Context?, appWidgetIds: IntArray?) {
        super.onDeleted(context, appWidgetIds)
    }

    override fun onDisabled(context: Context?) {
        super.onDisabled(context)
    }

}